/* Copyright 2017, 2018, 2019, 2020 Gabriel Czernikier
 *
 * This file is part of Côtehaus.
 * Côtehaus is free software: you can redistribute it and/or modify
 * it under the terms of one of the following:
 * 1. The same dual license as that of Côtehaus itself, of which individual licenses are
 * the MIT License and the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 2. The MIT License.
 * 3. The GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * Côtehaus is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * MIT License and the GNU General Public License for more details.
 * You should have received a copy of the MIT License and the GNU General Public License
 * along with Côtehaus.  If not, see <https://opensource.org/licenses/MIT> and <http://www.gnu.org/licenses/>.
 */
#define _BSD_SOURCE
#include <endian.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "composer.h"
#include "lgwseq.h"
#include "types.h"

LIST_HEAD(str_id_head);
unsigned int str_id_list_size = 0;

struct ostr_id_st {
	struct str_id_st *str_id;
	struct ostr_id_st *next_representative;
	unsigned int relative_offset;
} *ostr_id_ary;

struct str_id_st *add_str_id(lgwseq_t *for_str_dt) {
	struct str_dt_st *str_dt = malloc(sizeof(struct str_dt_st));
	str_dt->unpacked_data=lgwseq;
	struct str_id_st *str_id = malloc(sizeof(struct str_id_st));
	str_id->str_dt = str_dt;
	list_add(&str_id->str_id_list, &str_id_head);
	str_id_list_size++;
	return str_id;
}

int ostr_id_st_compar(struct ostr_id_st *ostr_id_1, struct ostr_id_st *ostr_id_2) {
	int cmp = lgwseq_t_compar(ostr_id_1->str_id->str_dt->unpacked_data, ostr_id_2->str_id->str_dt->unpacked_data);
	return cmp;
}

void build_ostr_id_and_pack_str_dt() {
	/* Walkthrough over str_dt_list is expensive.
	 * Making both sorting and packing in one pass tries to come over the penalty.
	 * Not that both transformations are really related one to the other, however.
	 */
	ostr_id_ary = malloc((str_id_list_size+1/*reserves one more position*/)*sizeof(struct ostr_id_st));
	memset(ostr_id_ary, 0, (str_id_list_size+1)*sizeof(struct ostr_id_st));
	// Prepare an artificial element that when used to compute the relative_offset, the result is correct.
	ostr_id_ary->relative_offset =0;
	// Conceal the temporarily intended array position (care of that upon memory deallocation, if ever performed).
	ostr_id_ary++;
	struct list_head *tmp;
	unsigned int i=0;
	list_for_each(tmp, &str_id_head) {
		(ostr_id_ary+i)->str_id = (struct str_id_st *)list_entry(tmp, struct str_id_st, str_id_list);
		i++;
	}
	qsort(ostr_id_ary, str_id_list_size, sizeof(struct ostr_id_st), (int (*)(const void *, const void *))ostr_id_st_compar);
	i=1;
	unsigned int representative_i = 0, representative_idx = 0;
	size_t packed_size=0;
	while(representative_i!=str_id_list_size) {
		(ostr_id_ary+representative_i)->relative_offset = (ostr_id_ary+representative_i-1)->relative_offset+packed_size;
		packed_size = pack_string_data((ostr_id_ary+representative_i)->str_id->str_dt->unpacked_data, &(ostr_id_ary+representative_i)->str_id->str_dt->packed_data);
		BUFFER_WRITE(buffer[NH_STR_DT_IDX],buf_len[NH_STR_DT_IDX],(ostr_id_ary+representative_i)->str_id->str_dt->packed_data,buf_offset[NH_STR_DT_IDX],packed_size)
		(ostr_id_ary+representative_i)->str_id->idx = representative_idx;
		while(i<str_id_list_size && lgwseq_t_compar((ostr_id_ary+representative_i)->str_id->str_dt->unpacked_data,(ostr_id_ary+i)->str_id->str_dt->unpacked_data)==0) {
			(ostr_id_ary+i)->relative_offset = (ostr_id_ary+representative_i)->relative_offset;
			(ostr_id_ary+i)->str_id->idx = (ostr_id_ary+representative_i)->str_id->idx;
			i++;
		}
		if(i!=str_id_list_size)
			(ostr_id_ary+representative_i)->next_representative = ostr_id_ary+i;
		representative_i=i++;
		representative_idx++;
	}
	bounds_move(NH_STR_ID_IDX, sizeof(uint32_t)/*Storage Designator: (struct ostr_id_st).relative_offset+up_bounds_array[i]*/*representative_idx);
	bounds_move(NH_STR_DT_IDX, (ostr_id_ary+representative_i-1)->relative_offset + packed_size);
	majors_size_ary[NH_STR_ID_IDX] = representative_idx;
}

void pack_ostr_id_ary() {
	for(struct ostr_id_st *ostr_id_representative = ostr_id_ary;ostr_id_representative!=NULL; ostr_id_representative=ostr_id_representative->next_representative){
		uint32_t string_data_off = htole32(ostr_id_representative->relative_offset+up_bounds_ary[NH_STR_DT_IDX-1]);
		BUFFER_WRITE(buffer[NH_STR_ID_IDX],buf_len[NH_STR_ID_IDX],&string_data_off,buf_offset[NH_STR_ID_IDX],sizeof(string_data_off));
	}
}


